package com.eims.sls_app.ui;

import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.eims.sls_app.dataaccess.Lottery;
import com.eims.sls_app.utils.App;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by juejinkaifa on 2018/4/1.
 */

public class MyFragmentActivity extends FragmentActivity {


    long startTime = 0l;
    long endTime = 0l;

    /**
     * 序列化对象
     *
     * @param -person
     * @return
     * @throws IOException
     */
    private String serialize(Lottery lottery) throws IOException {
        startTime = System.currentTimeMillis();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                byteArrayOutputStream);
        objectOutputStream.writeObject(lottery);
        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
        objectOutputStream.close();
        byteArrayOutputStream.close();
        Log.e("serial", "serialize str =" + serStr);
        endTime = System.currentTimeMillis();
        Log.e("serial", "序列化耗时为:" + (endTime - startTime));
        return serStr;
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private Lottery deSerialization(String str) throws IOException,
            ClassNotFoundException {
        startTime = System.currentTimeMillis();
        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                redStr.getBytes("ISO-8859-1"));
        ObjectInputStream objectInputStream = new ObjectInputStream(
                byteArrayInputStream);
        Lottery person = (Lottery) objectInputStream.readObject();
        Log.e("---",person.toString());
        objectInputStream.close();
        byteArrayInputStream.close();
        endTime = System.currentTimeMillis();
        Log.e("serial", "反序列化耗时为:" + (endTime - startTime));
        return person;
    }

    void saveObject(String strObject) {
        SharedPreferences sp = getSharedPreferences("Lottery", 0);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("Lottery", strObject);
        edit.commit();
    }

    public  String getObject() {
        SharedPreferences sp = getSharedPreferences("Lottery", 0);
        return sp.getString("Lottery", null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        try{
            saveObject(serialize(App.lottery));
        }catch ( Exception e){

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            App.lottery=deSerialization(getObject());
        }catch ( Exception e){

        }


    }
}
